package com.ilove.friend.frame;

import java.io.IOException;

import javax.microedition.lcdui.Image;
import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.core.model.DataProvider;
import org.kalmeo.kuix.util.Metrics;
import org.kalmeo.kuix.widget.Gauge;
import org.kalmeo.kuix.widget.ListItem;
import org.kalmeo.kuix.widget.Picture;
import org.kalmeo.kuix.widget.PopupBox;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.kuix.widget.Text;
import org.kalmeo.kuix.widget.Widget;
import org.kalmeo.kuix.widget.Screen.ScreenMenu;
import org.kalmeo.util.MathFP;
import org.kalmeo.util.frame.Frame;

import com.ilove.friend.model.FriendContants;
import com.ilove.friend.model.FriendDataProvider;
import com.ilove.friend.model.ProvinceList;

public class ProvinceFrame implements Frame {

	public static ProvinceFrame instance = new ProvinceFrame();
	public String sex;// 0 woman, 1 man, 2 all
	public String[] provincelist = null;
	int currPage = 1;
	int pageSize = 3;
	int totalpage = 1;
	boolean isnext = true;
	private final FriendDataProvider dp = new FriendDataProvider();
	private Screen screen;
	private Gauge progressGauge;
	public void showScreen() {
		if (screen != null) {
			screen.setCurrent();
		}
	}

	public void onAdded() {
		try {
			pageSize = FriendContants.pageSize;
			int total = dp.getTotalProvince();
			totalpage = (int) Math.ceil((double)total/(double)pageSize);
			screen = Kuix.loadScreen(FriendContants.PROVINCEFRAME_SCREEN, dp);
			changePageValue(currPage);
			screen = Kuix.loadScreen(FriendContants.PROVINCEFRAME_SCREEN, dp);
			screen.setCurrent();
			Text textPage = (Text)screen.getWidget("textPage");
			textPage.setText(currPage + "/" + totalpage);
			int screenWidth = Kuix.getCanvas().getWidth();
			ScreenMenu rightMenu = (ScreenMenu)screen.getSecondMenu();
			Metrics metrics = rightMenu.getPreferredSize(screenWidth);
			int containerWidth = screenWidth - (2*metrics.width)-10;
			Widget container = screen.getWidget("CTM_PAGE");
			container.setAttribute("style", "layout:flowlayout(true, fill);align:center;min-size:"+containerWidth + " 16");
		} catch (Exception e) {
			Kuix.alert(e);
			e.printStackTrace();
		}
	}

	public boolean onMessage(Object identifier, Object[] arguments) {
		if ("next".equals(identifier)) {
			Widget focusedWidget = screen.getFocusManager().getFocusedWidget();
			if (focusedWidget instanceof ListItem) {
				DataProvider dataprovider = ((ListItem) focusedWidget)
						.getDataProvider();
				if (dataprovider != null) {
					ProvinceList provincelist = (ProvinceList) dataprovider;
					try {
						FriendListFrame.instance.sex = sex;
						FriendListFrame.instance.provincecode = provincelist.code;
						FriendListFrame.instance.provincename = provincelist.name;
						Kuix.getFrameHandler().pushFrame(
								FriendListFrame.instance);

					} catch (Exception e) {
					}
				}
			}
		} else if ("new".equals(identifier)) {
			Widget focusedWidget = screen.getFocusManager().getFocusedWidget();
			if (focusedWidget instanceof ListItem) {
				DataProvider dataprovider = ((ListItem) focusedWidget)
						.getDataProvider();
				if (dataprovider != null) {
					ProvinceList provincelist = (ProvinceList) dataprovider;
					try {
						TopNewFrame.instance.sex = sex;
						TopNewFrame.instance.provincecode = provincelist.code;
						TopNewFrame.instance.provincename = provincelist.name;
						TopNewFrame.instance.type = "0";
						Kuix.getFrameHandler().pushFrame(TopNewFrame.instance);
					} catch (Exception e) {
					}
				}
			}
		} else if ("hot".equals(identifier)) {
			Widget focusedWidget = screen.getFocusManager().getFocusedWidget();
			if (focusedWidget instanceof ListItem) {
				DataProvider dataprovider = ((ListItem) focusedWidget)
						.getDataProvider();
				if (dataprovider != null) {
					ProvinceList provincelist = (ProvinceList) dataprovider;
					try {

						TopNewFrame.instance.sex = sex;
						TopNewFrame.instance.provincecode = provincelist.code;
						TopNewFrame.instance.provincename = provincelist.name;
						TopNewFrame.instance.type = "1";
						Kuix.getFrameHandler().pushFrame(TopNewFrame.instance);

					} catch (Exception e) {
					}
				}
			}
		} else if ("back".equals(identifier)) {
			Kuix.getFrameHandler().removeFrame(instance);
			return false;
		} else if ("arrLeft".equals(identifier)) {
			if (currPage > 1)
				currPage--;
			try {
				changePageValue(currPage);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if ("arrRight".equals(identifier)) {
			if (isnext) {
				currPage++;
				try {
					changePageValue(currPage);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return false;
	}

	public void onRemoved() {
		try {
			currPage = 1;
			FriendMainFrame.instance.showScreen();
			screen.cleanUp();
			screen = null;
		} catch (Exception e) {
		}
	}

	public void changePageValue(int page) throws Exception {

		Text textPage = (Text) screen.getWidget("textPage");
		textPage.setText(page + "/" + totalpage);
		Picture pictureLeft = (Picture) screen.getWidget("pic-left");
		Picture pictureRight = (Picture) screen.getWidget("pic-right");
		try {
			if (page == 1) {
				pictureLeft.setImage(Image.createImage("/img/left-dis.png"));
			} else {
				pictureLeft.setImage(Image.createImage("/img/left.png"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			// get data form
			provincelist = dp.getProvinceListWithPage(page, pageSize);
			System.out.println(provincelist.length);
			if (provincelist.length != 0) {
				dp.setCategoryList(provincelist,progressGauge);
			}
			// display
			if (provincelist.length < pageSize) {
				isnext = false;
				pictureRight.setImage(Image.createImage("/img/right-dis.png"));
			} else {
				isnext = true;
				pictureRight.setImage(Image.createImage("/img/right.png"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
